class Enemy{
    constructor(){
        this.frameX=0;
        this.frameY=0;
        this.fps=20;
        this.timeInterval=1000/this.fps;
        this.timer=0;
        this.deleteFlag=false;

    }
    update(delatTime){
        this.x-=this.speedX+this.game.speed;
        this.y+=this.speedY;
        if(this.x+this.width<0)
            this.deleteFlag=true;
        
        if(this.timer>this.timeInterval){
            if(this.frameX>this.maxFrame)
                this.frameX=0;
            else this.frameX++;
            this.timer=0
        }else{
            this.timer+=delatTime;
        }
        


    }
    draw(context){
        context.drawImage(this.img,this.frameX*this.width,this.frameY*this.height,this.width,this.height,this.x,this.y,this.width,this.height)
    }
}
export class FlyingEnemy extends Enemy{
    constructor(game){
        super();
        this.game=game;
        this.width=60;
        this.height=44;
        this.x=this.game.width;
        this.y=Math.random()*this.game.height*0.5;
        this.speedX=Math.random()*2+1;
        this.speedY=0;
        this.angle=0;
        this.angleSpeed=Math.random()*0.1+0.05;
        this.maxFrame=4;
        this.img=document.getElementById('enemy_fly')
    }
    update(delatTime){
        super.update(delatTime)
        this.angle+=this.angleSpeed;
        this.y+=Math.sin(this.angle);
    }
    draw(context){
        super.draw(context)
    }

}
export class GroundEnemy extends Enemy{
    constructor(game){
        super();
        this.game=game;
        this.width=60;
        this.height=87;
        this.x=this.game.width;
        this.y=this.game.height-this.height-this.game.groundMargin;
        this.speedX=0;
        this.speedY=0;      
        this.maxFrame=0;
        this.img=document.getElementById('enemy_plant')
    }
    update(delatTime){
        super.update(delatTime)

    }
    draw(context){
        super.draw(context)
    }}
export class ClimbingEnemy extends Enemy{
    constructor(game){
        super();
        this.game=game;
        this.width=120;
        this.height=144;
        this.x=this.game.width;
        this.y=Math.random()*this.game.height*0.5;
        this.speedX=0;
        this.speedY=Math.random()>0.5?1:-1;      
        this.maxFrame=4;
        this.img=document.getElementById('enemy_spider')
    }
    update(delatTime){
        super.update(delatTime)
        //蜘蛛触底回拉
        if(this.y>this.game.height-this.height-this.game.groundMargin)
            this.speedY*=-1;
        if(this.y+this.height<0) this.deleteFlag=true;


    }
    draw(context){
        super.draw(context)
        //蜘蛛丝
        context.beginPath();
        context.moveTo(this.x+this.width*0.5,0);
        context.lineTo(this.x+this.width*0.5,this.y+50);
        context.stroke();
    }
}